package com.ruoyi.system.Fdongbei.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
<<<<<<< HEAD
import com.ruoyi.common.core.domain.AjaxResult;
=======
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.Fdongbei.domain.TbGuozhong;
import com.ruoyi.system.Fdongbei.domain.TbMerge;
import com.ruoyi.system.Fdongbei.domain.TbSuoshugongsi;
import com.ruoyi.system.Fdongbei.domain.TbWx;
import com.ruoyi.system.Fdongbei.domain.dibang.GuozhongDo;
<<<<<<< HEAD
import com.ruoyi.system.Fdongbei.domain.dibang.MenjinDo;
=======
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
import com.ruoyi.system.Fdongbei.domain.dibang.TbMMGuozhong;
import com.ruoyi.system.Fdongbei.domain.vo.reqvo.CarInfoReqVo;
import com.ruoyi.system.Fdongbei.mapper.TbMergeMapper;
import com.ruoyi.system.Fdongbei.mapper.TbSuoshugongsiMapper;
import com.ruoyi.system.Fdongbei.mapper.TbWxMapper;
<<<<<<< HEAD
import com.ruoyi.system.Fdongbei.mapper.dibang.MenjinDoMapper;
=======
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
import com.ruoyi.system.Fdongbei.mapper.dibang.TbMMGuozhongMapper;
import com.ruoyi.system.Fdongbei.service.ITbMergeService;
import org.checkerframework.checker.regex.qual.Regex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.Fdongbei.domain.dibang.TbMMGuozhong;

import javax.annotation.Resource;


/**
 * 多交货单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-07
 */
@Service
@DataSource(value = DataSourceType.LY002_L)
public class TbMergeServiceImpl implements ITbMergeService
{
    @Resource
    private TbMergeMapper tbMergeMapper;
<<<<<<< HEAD
    @Resource
    private MenjinDoMapper menjinDoMapper;
=======

>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @Resource
    TbSuoshugongsiMapper tbSuoshugongsiMapper;

    @Resource
    TbWxMapper tbWxMapper;

    @Resource
    TbMMGuozhongMapper tbMMGuozhongMapper;
    /**
     * 查询多交货单
     * 
     * @param czbh 多交货单主键
     * @return 多交货单
     */
    @Override
    public TbMerge selectTbMergeByCzbh(String czbh)
    {
        return tbMergeMapper.selectTbMergeByCzbh(czbh);
    }

    /**
     * 查询多交货单列表
     * 
     * @param tbMerge 多交货单
     * @return 多交货单
     */
    @Override
    public List<TbMerge> selectTbMergeList(TbMerge tbMerge)
    {
        return tbMergeMapper.selectTbMergeList(tbMerge);
    }

    @Override
    public List<TbMMGuozhong> selectTbMergeListGuozhong(TbMMGuozhong tbGuozhong) {
        return tbMergeMapper.getCornCarShInfoGuozhong(tbGuozhong);
    }
     /**
     * 新增多交货单
     *
     * @param tbMMGuozhong 多交货单
     * @return 结果
     */
    @Override
    public int insertTbMerge(TbMMGuozhong tbMMGuozhong) {

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String czbh = generateSerialNumber();
        TbMerge tbMerge = new TbMerge(
                czbh,
                czbh,
                tbMMGuozhong.getChehaomm(),
                tbMMGuozhong.getSdfbh(),
                tbMMGuozhong.getShoudf(),
                tbMMGuozhong.getGsbh(),
                tbMMGuozhong.getGsmc(),
                tbMMGuozhong.getWlbh(),
                tbMMGuozhong.getWlmc(),
                tbMMGuozhong.getCkbh(),
                tbMMGuozhong.getCkmc(),
                tbMMGuozhong.getYzzl(),
                tbMMGuozhong.getYzkz(),
                tbMMGuozhong.getGblx(),
                "待进厂",
                "正常",
                tbMMGuozhong.getDdwlsl(),
                tbMMGuozhong.getChexc(), // 制卡人
                tbMMGuozhong.getSd_dwx(),
                tbMMGuozhong.getSd_bzwmc(),
                now.format(formatter),
                tbMMGuozhong.getDdbh(),
                tbMMGuozhong.getCzbh(),
                tbMMGuozhong.getDdbh());

        TbMMGuozhong uptbguozhong2 = new TbMMGuozhong();
        uptbguozhong2.setCard_id(czbh);
        uptbguozhong2.setCzbh(tbMMGuozhong.getCzbh());
        // 更新过重称重编号
        tbMMGuozhongMapper.updateTbGuozhong(uptbguozhong2);
        // 所属公司插入
        tbSuoshugongsiMapper.insertTbsuoshugongsi(new TbSuoshugongsi(czbh, "东北"));
        // 微信表信息插入
        tbWxMapper.insertWx(new TbWx(0, tbMMGuozhong.getShoujihao(), czbh, tbMMGuozhong.getChehaomm()));
        return tbMergeMapper.insertTbMerge(tbMerge);
    }


//    @Override
//    public int insertTbMerge(tbMMGuozhong tbMMGuozhong)
//    {
//        return tbMergeMapper.insertTbMerge(tbMerge);
//    }

    /**
     * 多交货单非首次添加
     * 
     * @param tbMMGuozhong 多交货单
     * @return 结果
     */
    @Override
    public int updateTbMerge(TbMMGuozhong tbMMGuozhong)
    {
        String chehao = tbMMGuozhong.getChehao().replaceAll("\\.","");
        TbMerge tbMerge = tbMergeMapper.selectTbMergeByCzbh(tbMMGuozhong.getShoujihao());
        // 判断车号跟 客户编号是否一致
        if (!(tbMerge.getKhbh().equals(tbMMGuozhong.getSdfbh())) ||  !(chehao.equals(tbMerge.getChehao()))){
            return 0;
        }
        // 物料数量求和
        tbMerge.setDdwlsl(Integer.parseInt(tbMerge.getDdwlsl()) + Integer.parseInt(tbMMGuozhong.getDdwlsl())+"");
        String merge_jhd = tbMerge.getDdbh()+"-"+tbMMGuozhong.getDdbh();
        String gz_czbh = tbMerge.getGzCzbh()+ "|" + tbMMGuozhong.getCzbh();
        tbMerge.setDdbh(merge_jhd);
        tbMerge.setGzCzbh(gz_czbh);
        tbMerge.setMergeJhd(merge_jhd);
        // 更新过重称重编号
        TbMMGuozhong tbmm = new TbMMGuozhong();
        tbmm.setCzbh(tbMMGuozhong.getCzbh());
        tbmm.setCard_id(tbMMGuozhong.getShoujihao());
        tbMMGuozhongMapper.updateTbGuozhong(tbmm);

        return tbMergeMapper.updateTbMerge(tbMerge);
    }

    /**
     * 更新打印次数
     * @param  czbh 更新打印次数
     * @return
     */
    @Override
    public int updateTbMergeDb(String czbh) {
        return tbMergeMapper.updateTbMergeDdbdcs(czbh);
    }

<<<<<<< HEAD
    @Override
    public int updateTbMergewh(TbMerge tbMerge) {
        return tbMergeMapper.updateTbMerge(tbMerge);
    }

=======
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    /**
     * 批量删除多交货单
     * 
     * @param czbhs 需要删除的多交货单主键
     * @return 结果
     */
    @Override
    public int deleteTbMergeByCzbhs(String[] czbhs)
    {
        return tbMergeMapper.deleteTbMergeByCzbhs(czbhs);
    }

<<<<<<< HEAD
    @Override
    public AjaxResult menjinModifyStatus(TbMerge tbMerge) {

        MenjinDo menjinDo = menjinDoMapper.selectByCzbh(tbMerge.getCzbh());


        if (!tbMerge.getGbzt().equals("完成")) {
            return AjaxResult.error("车辆未完成过磅");
        }

        if (menjinDo == null){
            return AjaxResult.error("无此车辆门禁记录，无法操作!!");
        }
        String cczt = menjinDo.getCczt();
        if (cczt == null) {
            cczt = "出厂";
            menjinDo.setCczt(cczt);
            int i = menjinDoMapper.updateMenjinStatuxByCzbh(menjinDo);
            if (i > 0) {
                return AjaxResult.success("修改车辆状态为，完成，已出厂。");
            }
        } else {
            cczt = null;
            menjinDo.setCczt(cczt);
            int i = menjinDoMapper.updateMenjinStatuxByCzbh(menjinDo);
            if (i > 0) {
                return AjaxResult.success("修改车辆状态为，完成，未出厂。");
            }
        }

        return AjaxResult.error("修改失败");
    }

=======
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    /**
     * 删除多交货单信息
     * 
     * @param czbh 多交货单主键
     * @return 结果
     */
    @Override
    public int deleteTbMergeByCzbh(String czbh)
    {
        return tbMergeMapper.deleteTbMergeByCzbh(czbh);
    }

    /**
     * 生成card_id
     * @return
     */
    public String generateSerialNumber() {

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 获取当天最新称重编号
        String czbhm = tbMergeMapper.selectMaxCzbh(currentDate + " 00:00:00",currentDate + " 23:59:59");
        // 获取当前的年、月和日
        String dataFrom = currentDate.format(formatter);
        String serialNumber = "";
        // 生成完整的编号
        if (czbhm != null){
            czbhm = Integer.valueOf(czbhm)+1+"";
            String incrementPart = String.format("%02d", Integer.valueOf(czbhm));
            serialNumber = "DC"+dataFrom+incrementPart;
            // 格式化计数器为4位数（例如：0001, 0002, ...）

        }else {

            serialNumber = "DC" + dataFrom + "01";
        }

        return serialNumber;
    }

}
